package com.tool.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;

import java.io.*;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author: zhoubin
 * @date: 2024/1/5
 */
public class FileUtil {

    public static String abPath(String path) {

        return FileUtil.class.getClassLoader().getResource(path).getPath();
    }

    /**
     * 将字符串内容填充到文件中。
     *
     * @param filePath
     * @param consumer
     */
    public static void writeTxt(String filePath, Consumer<BufferedWriter> consumer) {

        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(filePath));
            consumer.accept(bw);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 读取文件中的内容
     * @param filePath
     * @param consumer
     */
    public static void readTxt(String filePath, Consumer<String> consumer) {

        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(filePath));

            String line;

            while ((line = br.readLine()) != null) {
                consumer.accept(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 读取文档内容
     *
     * @param path
     * @return
     */
    public static String readFile(String path) {
        BufferedReader bis = null;
        try {
            FileReader fis = new FileReader(path);
            bis = new BufferedReader(fis);

            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = bis.readLine()) != null) {
                sb.append(line).append("\n");
            }

            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }

    /**
     * 将对象转为json存到文件中.
     *
     * @param filePath 文件路径
     * @param t 对象
     * @param <T> 泛型
     */
    public static <T> void writeObjcectAsJson(String filePath, T t) {
        String json = null;
        json = JsonUtil.toJsonStr(t);
        writeStrToFile(filePath, json);
    }
    /**
     * 保存字符串到文件中
     *
     * @param filePath
     * @param str
     */
    public static void writeStrToFile(String filePath, String str) {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(filePath));

            bw.write(str);
            System.out.println("对象字符串成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 将文件中的json串还原为对象
     * @param filePath
     * @param beanClass
     * @param <T>
     * @return
     */
    public static <T> T readJsonObject(String filePath, Class<T> beanClass) {
        BufferedReader br = null;
        try {
            String json = readFile(filePath);

            T bean = JsonUtil.toObject(json, beanClass);
            return bean;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;

    }

    /**
     * 将文件中的json串还原为对象
     * @param filePath
     * @param beanClass
     * @param <T>
     * @return
     */
    public static <T> List<T> readJsonObjectToList(String filePath, Class<T> beanClass) {

        String json = readFile(filePath);

        return JsonUtil.toObjList(json, beanClass);
    }

    /**
     * 将对象序列化到文件中
     *
     * @param filePath
     * @param t
     * @param <T>
     */
    public static <T> void writeObjcect(String filePath, T t) {
        try {
            ObjectOutputStream ooStream = new ObjectOutputStream(new FileOutputStream(filePath));

            ooStream.writeObject(t);
            ooStream.close();
            System.out.println("对象序列化成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 从文件中读取序列化对象.
     *
     * @param filePath
     * @return
     */
    public static Object ReadObject(String filePath) {
        ObjectInputStream objectInputStream;
        try {
            objectInputStream = new ObjectInputStream(new FileInputStream(filePath));
            Object obj = objectInputStream.readObject();
            objectInputStream.close();
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
}
